package xyz.migle.sc.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ScApp003{
    private static final Logger logger = LoggerFactory.getLogger(ScApp003.class);

    @Autowired
    LoadBalancerClient loadBalancerClient;
    //@Autowired
    RestTemplate restTemplate = new RestTemplate();

    @RequestMapping("/")
    public String home() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("sc-demo-base-001");
        //String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/dc";
        String url = serviceInstance.getUri().toString();
        logger.error(url);
        return "i am sc-demo-app-001,i depend on "+ url +":" + restTemplate.getForObject(url, String.class);

    }

//    @Bean
//    public RestTemplate restTemplate() {
//        return new RestTemplate();
//    }

    public static void main(String[] args) {
         SpringApplication.run(ScApp003.class,args);
    }

}

